//
//  JKCellStoreSections.m
//  chequanbao_ios
//
//  Created by 朱凯 on 2018/1/29.
//  Copyright © 2018年 com.itboye. All rights reserved.
//

#import "JKCellStoreIndexSections.h"

@implementation JKCellStoreIndexSections

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    NSArray *titles = @[@"生活服务", @"美食美酒", @"珠宝配饰", @"运动户外"];
    __block UIControl *last = nil;
    [titles enumerateObjectsUsingBlock:^(NSString *title, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIControl *basalView = [UIControl new];
        basalView.tag = idx;
        [basalView addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:basalView];
        
        UIImageView *imageView = [UIImageView new];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"商城-%@", title]];
        [basalView addSubview:imageView];
        
        UILabel *label = [[UILabel alloc] init];
        label.adjustsFontSizeToFitWidth = true;
        label.text = title;
        label.font = FONT(15);
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor blackColor];
        [basalView addSubview:label];
        
        
        [basalView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@0);
            make.width.equalTo(self.contentView).multipliedBy(0.25);
            if (last) {
                make.left.equalTo(last.mas_right);
            } else {
                make.left.equalTo(@0);
            }
            
            // 设置cell的高度
            make.bottom.equalTo(@0);
        }];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(@50);
            make.top.equalTo(@10);
            make.centerX.equalTo(@0);
        }];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(@0);
            make.top.equalTo(imageView.mas_bottom).offset(10);
            make.height.equalTo(@20);
            // 设置basalView的高度
            make.bottom.equalTo(@-10);
        }];
        
        last = basalView;
    }];
}

- (void)tapAction:(UIControl *)sender {
    
    if (_tapBlock) {
        _tapBlock(sender.tag);
    }
}


@end
